
package common;

/**
 * Owns a physicsWorld and implements part of the runnable code for the physics 
 * thread both for the client and the server. 
 *
 * @author xissburg
 */
public class PhysicsThread implements Runnable
{
    protected PhysicsWorld physicsWorld;
    private int stepsPerSecond;
    private volatile boolean running;

    public PhysicsThread()
    {
        physicsWorld = new PhysicsWorld();
        stepsPerSecond = 100;
        running = true;
    }

    public int getStepsPerSecond() {
        return stepsPerSecond;
    }

    public void setStepsPerSecond(int stepsPerSecond) {
        this.stepsPerSecond = stepsPerSecond;
    }

    public PhysicsWorld getPhysicsWorld() {
        return physicsWorld;
    }

    public void run()
    {
        running = true;
        physicsWorld.start();

        while(running)
        {
            preStep();
            physicsWorld.step();
            postStep();

            try {
                Thread.sleep(1000/stepsPerSecond);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void stop() {
        running = false;
    }

    protected void preStep() {}
    protected void postStep() {}
}
